package com.xydtech.ecard.ecard.service.impl;

import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.custgroup.domain.EcardApiCustomerGroupMerchant;
import com.xydtech.ecard.custgroup.mapper.EcardApiCustomerGroupMerchantMapper;
import com.xydtech.ecard.ecard.domain.EcardApiAgricultureInfo;
import com.xydtech.ecard.ecard.domain.EcardApiMerchantTask;
import com.xydtech.ecard.ecard.mapper.EcardApiAgricultureInfoMapper;
import com.xydtech.ecard.ecard.mapper.EcardApiMerchantTaskMapper;
import com.xydtech.ecard.ecard.service.IEcardApiAgricultureInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 电子名片新型农业主体信息Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-08-19 上午10:51
 */
@Service
public class EcardApiAgricultureInfoServiceImpl implements IEcardApiAgricultureInfoService {

    @Autowired
    private EcardApiAgricultureInfoMapper ecardApiAgricultureInfoMapper;
    @Autowired
    private EcardApiMerchantTaskMapper ecardMerchantTaskMapper;
    @Autowired
    private EcardApiCustomerGroupMerchantMapper ecardCustomerGroupMerchantMapper;

    /**
     * 条件查询我的新型农业主体列表
     * @param ecardAgricultureInfo
     * @return
     */
    @Override
    public List<EcardApiAgricultureInfo> getMyAgricultureList(EcardApiAgricultureInfo ecardAgricultureInfo, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardAgricultureInfo.getContactsPhone())) {
                ecardAgricultureInfo.setContactsPhone(AESEncryptionUtils.encrypt(ecardAgricultureInfo.getContactsPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardAgricultureInfo.getAgrCustPhone())) {
                ecardAgricultureInfo.setAgrCustPhone(AESEncryptionUtils.encrypt(ecardAgricultureInfo.getAgrCustPhone(), salt));
            }
            List<EcardApiAgricultureInfo> agricultureList = ecardApiAgricultureInfoMapper.getMyAgricultureList(ecardAgricultureInfo);
            if (agricultureList.size() > 0) {
                for (EcardApiAgricultureInfo agricultureInfo : agricultureList) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(agricultureInfo.getContactsPhone())) {
                        agricultureInfo.setContactsPhone(AESEncryptionUtils.decrypt(agricultureInfo.getContactsPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(agricultureInfo.getAgrCustPhone())) {
                        agricultureInfo.setAgrCustPhone(AESEncryptionUtils.decrypt(agricultureInfo.getAgrCustPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(agricultureInfo.getAgrPhoneNumber())) {
                        agricultureInfo.setAgrPhoneNumber(AESEncryptionUtils.decrypt(agricultureInfo.getAgrPhoneNumber(), salt));
                    }
                }
            }
            return agricultureList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询电子名片新型农业主体列表
     * @param ecardAgricultureInfo
     * @return
     */
    @Override
    public List<EcardApiAgricultureInfo> getAgricultureList(EcardApiAgricultureInfo ecardAgricultureInfo) {
        return ecardApiAgricultureInfoMapper.getAgricultureList(ecardAgricultureInfo);
    }

    /**
     * 查询新型农业主体地图坐标列表接口
     * @param creatorId
     * @param deptId
     * @param agrType
     * @return
     */
    @Override
    public List<EcardApiAgricultureInfo> getAgricultureCoordinateList(Long creatorId, Long deptId, Integer agrType) {
        return ecardApiAgricultureInfoMapper.getAgricultureCoordinateList(creatorId, deptId, agrType);
    }

    /**
     * 根据法人机构ID查询机构下所有新型农业主体坐标信息列表
     * @param deptId
     * @param agrType
     * @return
     */
    @Override
    public List<EcardApiAgricultureInfo> getAllAgricultureCoordinateList(Long creatorId, Long deptId, Integer agrType) {
        return ecardApiAgricultureInfoMapper.getAllAgricultureCoordinateList(creatorId, deptId, agrType);
    }

    /**
     * 查询电子名片新型农业主体信息
     * @param id
     * @return
     */
    @Override
    public EcardApiAgricultureInfo selectEcardAgricultureInfoById(Long id, String salt) {
        EcardApiAgricultureInfo agricultureInfo = ecardApiAgricultureInfoMapper.selectEcardAgricultureInfoById(id);
        try {
            if (agricultureInfo != null) {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(agricultureInfo.getAgrPhoneNumber())) {
                    agricultureInfo.setAgrPhoneNumber(AESEncryptionUtils.decrypt(agricultureInfo.getAgrPhoneNumber(), salt));
                }

            }
            return agricultureInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 唯一值验证，相同主体类型、支行、新型农业主体名不能重复
     * @param ecardAgricultureInfo
     * @return
     */
    @Override
    public int validateAgricultureInfo(EcardApiAgricultureInfo ecardAgricultureInfo) {
        return ecardApiAgricultureInfoMapper.validateAgricultureInfo(ecardAgricultureInfo);
    }

    /**
     * 保存电子名片新型农业主体信息
     * @param ecardAgricultureInfo
     * @return
     */
    @Override
    public int addAgricultureInfo(EcardApiAgricultureInfo ecardAgricultureInfo) {
        return ecardApiAgricultureInfoMapper.addAgricultureInfo(ecardAgricultureInfo);
    }

    /**
     * 根据ID修改新型农业主体状态
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changeAgricultureStatus(Long id) {
        //查询是否存在未处理营销任务
        EcardApiMerchantTask merchantTask = ecardMerchantTaskMapper.getMerchantTaskByMerchantId(id, "4");//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
        if (merchantTask!=null) {
            //存在则更新为已完成
            merchantTask.setTaskSts("1");
            ecardMerchantTaskMapper.updateEcardMerchantTask(merchantTask);
        }
        //同步修改行业客户中的状态
        EcardApiCustomerGroupMerchant groupMerchant = new EcardApiCustomerGroupMerchant();
        groupMerchant.setMerchantId(id);
        groupMerchant.setMerchantType("4");//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
        List<EcardApiCustomerGroupMerchant> customerGroupMerchantList = ecardCustomerGroupMerchantMapper.selectEcardCustomerGroupMerchantList(groupMerchant);
        if (customerGroupMerchantList.size() > 0) {
            ecardCustomerGroupMerchantMapper.changeIndustrialCustomerStatus(customerGroupMerchantList.get(0).getId());
        }
        return ecardApiAgricultureInfoMapper.changeAgricultureStatus(id);
    }
}
